package com.jwdf.uda.view.prompt;

import android.content.Context;
import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.ViewPromptUserBinding;
import com.jwdf.uda.utils.DisplayUtils;
import com.jwdf.uda.utils.PromptUtil;
import com.jwdf.uda.view.dialog.BaseDialog;

/**
 * 类说明：
 */
public class UserPrompt extends BaseDialog implements DialogInterface.OnDismissListener {
    private ViewPromptUserBinding mBinding;
    private View mGuideView;
    private int deviceWidth;
    private boolean mCancelable;

    public UserPrompt(Context context, View guideView) {
        super(context);
        this.mGuideView = guideView;
        this.deviceWidth = DisplayUtils.getDeviceWidth(context);
    }

    public UserPrompt(Context context, View guideView, boolean cancelable) {
        this(context, guideView);
        mCancelable = cancelable;
    }

    private Handler handler = new Handler();

    private Runnable mGuideRunnable = new Runnable() {
        @Override
        public void run() {
            int[] location = new int[2];
            mGuideView.getLocationOnScreen(location);
            location[1] -= DisplayUtils.getStatusBarHeight(getContext());
            int viewHeight = PromptUtil.getMeasuredHeight(mGuideView);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(deviceWidth, viewHeight);
            params.addRule(RelativeLayout.BELOW, mBinding.ivGuideTxt.getId());
            mBinding.ivGuide.setLayoutParams(params);

            mBinding.ivGuide.setVisibility(View.VISIBLE);
            mBinding.ivGuideTxt.setVisibility(View.VISIBLE);
            mBinding.ivGuideTxt.setPadding(DisplayUtils.dip2px(getContext(), 10), location[1] - DisplayUtils.dip2px(getContext(), 12), 0, DisplayUtils.dip2px(getContext(), 5));
        }
    };


    private Runnable mBtnRunnable = new Runnable() {
        @Override
        public void run() {
//            mBinding.ivBtn.setVisibility(View.VISIBLE);
//            mBinding.ivBtn.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    dismiss();
//                }
//            });
            mBinding.rlUserClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    };

    @Override
    protected View onInflateView(LayoutInflater inflater) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.view_prompt_user, null, false);
        mBinding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (View.VISIBLE == mBinding.ivBtn.getVisibility()) {
                    dismiss();
                }
            }
        });
        return mBinding.getRoot();
    }

    @Override
    protected void onInitView(View view) {
        setOnDismissListener(this);
        handler.postDelayed(mGuideRunnable, 500);
        handler.postDelayed(mBtnRunnable, 1000);
    }

    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = DisplayUtils.getDeviceWidth(getContext());
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        handler.removeCallbacks(mGuideRunnable);
        handler.removeCallbacks(mBtnRunnable);
        PromptUtil.setHideUserPrompt(getContext());
    }

    @Override
    public void onBackPressed() {
        if (mCancelable) {
            super.onBackPressed();
        }
    }
}
